package com.xzmzhp.dao.power.SQL;

import com.xzmzhp.pojo.power.RequestPath;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author 王源(xm)
 * @Date: 2020/12/9 08:23
 * @Description:
 */
public class RequestPathSQL {
    /**
     * 批量添加
     *
     * @param list 请求路径集合
     * @return 批量添加sql
     */
    public String addRequestPaths(List<RequestPath> list) {
        //定义StringBuilder
        StringBuilder stringBuilder = new StringBuilder(256);
        //添加 添加语句
        stringBuilder.append("insert into request_path (url) values ");
        //循环所有请求路径
        for (RequestPath requestPath : list) {
            stringBuilder.append("('")
                         .append(requestPath.getUrl())
                         .append("'),");
        }
        //删除最后一个逗号
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        //返回添加语句
        return stringBuilder.toString();
    }

    /**
     * 修改请求路径
     *
     * @param requestPath 请求路径实体类
     * @return 修改sql
     */
    public String updateRequestPath(RequestPath requestPath) {
        return new SQL() {
            {
                if (requestPath != null) {
                    UPDATE("request_path");
                    //请求路径字段
                    if (!"".equals(requestPath.getUrl()) && requestPath.getUrl() != null) {
                        SET("url ='" + requestPath.getUrl() + "'");
                    }
                    //请求路径描述字段
                    if (!"".equals(requestPath.getDescription()) && requestPath.getDescription() != null) {
                        SET("description ='" + requestPath.getDescription() + "'");
                    }
                    //请求路径id字段
                    if (requestPath.getId() != null && requestPath.getId() > 0) {
                        WHERE("id = " + requestPath.getId());
                    } else {
                        WHERE("id = " + 0);
                    }
                }
            }
        }.toString();
    }
}
